package com.simple.modules.sys.controller;

import com.alibaba.fastjson.JSON;
import com.simple.common.exception.RRException;
import com.simple.common.utils.EncryptUtil;
import com.simple.common.utils.PageUtils;
import com.simple.common.utils.R;
import com.simple.modules.sys.entity.SysUserEntity;
import com.simple.modules.sys.form.PasswordForm;
import com.simple.modules.sys.service.SysUserRoleService;
import com.simple.modules.sys.service.SysUserService;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 系统用户
 *
 *
 */
@RestController
@RequestMapping("/sys/sysUser")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 所有用户列表
     */
    @PostMapping("/getPage")
    public R getPage(@RequestBody Map<String, Object> params) {
        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取登录的用户信息
     */
    @GetMapping("/info")
    public R info() {
        return R.ok().put("user", getUser());
    }

    /**
     * 修改登录用户密码
     */
//    @SysLog("修改密码")
    @PostMapping("/updatePassword")
    public R password(@RequestBody PasswordForm form) {
        if (form.getNewPassword() == null || form.getNewPassword().isEmpty()) {
            throw new RRException("新密码不为能空");
        }
        String originNewPass = EncryptUtil.decode(form.getNewPassword());

        if (originNewPass.length() < 8) {
            throw new RRException("密码长度必须大于等于8");
        }
        Pattern pattern = Pattern.compile("^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*])[\\da-zA-Z~!@#$%^&*]{8,}$");
        Matcher matcher = pattern.matcher(originNewPass);
        if (!matcher.find()){
            throw new RRException("密码需要由数字,英文,特殊符号组成");
        }

        //sha256加密
        String password = new Sha256Hash(EncryptUtil.decode(form.getPassword()), getUser().getSalt()).toHex();
        //sha256加密
        String newPassword = new Sha256Hash(EncryptUtil.decode(form.getNewPassword()), getUser().getSalt()).toHex();

        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (!flag) {
            return R.error("原密码不正确");
        }

        return R.ok();
    }

    /**
     * 用户信息
     */
    @GetMapping("/getInfoByUserId")
    public R info(@RequestParam("userId") Long userId) {
        SysUserEntity user = sysUserService.getById(userId);

        //获取用户所属的角色列表
        List<Integer> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
//    @SysLog("新增用户")
    @PutMapping("/save")
    public R save(@RequestParam("user") String userJson) {
        SysUserEntity user = JSON.parseObject(userJson, SysUserEntity.class);
        user.setCreateUserId(getUserId());
        sysUserService.saveUser(user);

        return R.ok();
    }

    /**
     * 修改用户
     */
//    @SysLog("修改用户")
    @PutMapping("/update")
    public R update(@RequestBody SysUserEntity user) {
        user.setCreateUserId(getUserId());
        sysUserService.updateUser(user);
        return R.ok();
    }


    /**
     * 删除用户
     */
//    @SysLog("删除用户")
    @DeleteMapping("/delete")
    public R delete(@RequestBody List<Long> userIdList) {
        if (userIdList.contains(1L)) {
            return R.error("系统管理员不能删除");
        }
        if (userIdList.contains(getUserId())) {
            return R.error("当前用户不能删除");
        }

        sysUserService.removeByIds(userIdList);
        return R.ok();
    }

}
